home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 27 / CU Amiga Magazine's Super CD-ROM 27 (1998)(EMAP Images)(GB)[!][issue 1998-10].iso / CUCD / Programming / Mesa / src-glut / glutMainLoop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-02  |  8.0 KB  |  327 lines

  1. /*
  2.  * Amiga GLUT graphics library toolkit
  3.  * Version:  1.1
  4.  * Copyright (C) 1998 Jarno van der Linden
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Library General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Library General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Library General Public
  17.  * License along with this library; if not, write to the Free
  18.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21.  
  22. /*
  23.  * glutMainLoop.c
  24.  *
  25.  * Version 1.0  27 Jun 1998
  26.  * by Jarno van der Linden
  27.  * jarno@kcbbs.gen.nz
  28.  *
  29.  */
  30.  
  31.  
  32. #include <proto/intuition.h>
  33. #include <proto/gadtools.h>
  34. #include <proto/exec.h>
  35.  
  36. #include "glutstuff.h"
  37.  
  38.  
  39. extern void RedoMenu(int button, struct GlutMenu *glutmenu);
  40.  
  41.  
  42. int ConvRaw(UWORD code,UWORD qual)
  43. {
  44.     switch(code)
  45.     {
  46.         case CURSORLEFT:
  47.             return GLUT_KEY_LEFT;
  48.         case CURSORUP:
  49.             return GLUT_KEY_UP;
  50.         case CURSORRIGHT:
  51.             return GLUT_KEY_RIGHT;
  52.         case CURSORDOWN:
  53.             return GLUT_KEY_DOWN;
  54.         case 0x50:
  55.             return GLUT_KEY_F1;
  56.         case 0x51:
  57.             return GLUT_KEY_F2;
  58.         case 0x52:
  59.             return GLUT_KEY_F3;
  60.         case 0x53:
  61.             return GLUT_KEY_F4;
  62.         case 0x54:
  63.             return GLUT_KEY_F5;
  64.         case 0x55:
  65.             return GLUT_KEY_F6;
  66.         case 0x56:
  67.             return GLUT_KEY_F7;
  68.         case 0x57:
  69.             return GLUT_KEY_F8;
  70.         case 0x58:
  71.             return GLUT_KEY_F9;
  72.         case 0x59:
  73.             return GLUT_KEY_F10;
  74.     }
  75.  
  76.     return 0;
  77. }
  78.  
  79.  
  80. int ConvToButton(UWORD code,UWORD qual)
  81. {
  82.     switch(code)
  83.     {
  84.         case SELECTUP:
  85.         case SELECTDOWN:
  86.             if(qual & IEQUALIFIER_RCOMMAND)
  87.                 return GLUT_MIDDLE_BUTTON;
  88.             if(qual & IEQUALIFIER_RALT)
  89.                 return GLUT_RIGHT_BUTTON;
  90.             return GLUT_LEFT_BUTTON;
  91.         case MIDDLEUP:
  92.         case MIDDLEDOWN:
  93.             return GLUT_MIDDLE_BUTTON;
  94.         case MENUUP:
  95.         case MENUDOWN:
  96.             return GLUT_RIGHT_BUTTON;
  97.     }
  98.  
  99.     return 0;
  100. }
  101.  
  102.  
  103. int ConvToButtonState(UWORD code)
  104. {
  105.     switch(code)
  106.     {
  107.         case SELECTUP:
  108.         case MIDDLEUP:
  109.         case MENUUP:
  110.             return GLUT_UP;
  111.         case SELECTDOWN:
  112.         case MIDDLEDOWN:
  113.         case MENUDOWN:
  114.             return GLUT_DOWN;
  115.     }
  116.  
  117.     return 0;
  118. }
  119.  
  120.  
  121. void glutMainLoop(void)
  122. {
  123.     struct IntuiMessage *msg, cmsg;
  124.     struct GlutWindow *gw;
  125.     struct GlutMenu *gm;
  126.     struct GlutMenuEntry *gme;
  127.     struct MenuItem *item;
  128.     UWORD menuNumber;
  129.     BOOL idleing, wanttoquit;
  130.  
  131.     wanttoquit = FALSE;
  132.     while(1)
  133.     {
  134.         // Apply changes
  135.         //
  136.         for(gw = glutstuff.wins; gw; gw = gw->next)
  137.         {
  138.             stuffMakeCurrent(gw);
  139.  
  140.             if(gw->needposition)
  141.             {
  142.                 gw->needposition = FALSE;
  143.                 gw->needpositiongui = FALSE;
  144.                 // Let the change be made through the gui
  145.                 ChangeWindowBox(gw->window,gw->winx,gw->winy,gw->winwidth,gw->winheight);
  146.             }
  147.  
  148.             if(gw->needpositiongui)
  149.             {
  150.                 gw->needpositiongui = FALSE;
  151.             }
  152.  
  153.             if(gw->needreshape)
  154.             {
  155.                 gw->needreshape = FALSE;
  156.                 gw->needreshapegui = FALSE;        /* Reshaping and redisplaying is useless */
  157.                 gw->needredisplay = FALSE;        /* this round. Change is inevitable */
  158.                 // Let the change be made through the gui
  159.                 ChangeWindowBox(gw->window,gw->winx,gw->winy,gw->winwidth,gw->winheight);
  160.             }
  161.  
  162.             if(gw->needreshapegui)
  163.             {
  164.                 gw->needreshapegui = FALSE;
  165.                 if(gw->reshapefunc)
  166.                     gw->reshapefunc(WIN_WIDTH(gw->window), WIN_HEIGHT(gw->window));
  167.             }
  168.  
  169.             if(gw->needredisplay)
  170.             {
  171.                 gw->needredisplay = FALSE;
  172.                 if(gw->displayfunc)
  173.                     gw->displayfunc();
  174.             }
  175.             if(gw->needvisibility)
  176.             {
  177.                 gw->needvisibility = FALSE;
  178.                 if(gw->visibilityfunc)
  179.                     gw->visibilityfunc(gw->visible ? GLUT_VISIBLE : GLUT_NOT_VISIBLE);
  180.             }
  181.             if((gw->leftmenu && gw->leftmenu->needupdate) || gw->needleftmenu)
  182.             {
  183.                 gw->needleftmenu = FALSE;
  184.                 RedoMenu(GLUT_LEFT_BUTTON, gw->leftmenu);
  185.             }
  186.             if((gw->middlemenu && gw->middlemenu->needupdate) || gw->needmiddlemenu)
  187.             {
  188.                 gw->needmiddlemenu = FALSE;
  189.                 RedoMenu(GLUT_MIDDLE_BUTTON, gw->middlemenu);
  190.             }
  191.             if((gw->rightmenu && gw->rightmenu->needupdate) || gw->needrightmenu)
  192.             {
  193.                 gw->needrightmenu = FALSE;
  194.                 RedoMenu(GLUT_RIGHT_BUTTON, gw->rightmenu);
  195.             }
  196.         }
  197.  
  198.         for(gm = glutstuff.menus; gm; gm = gm->next)
  199.         {
  200.             stuffMakeCurrentMenu(gm);
  201.             gm->needupdate = FALSE;
  202.         }
  203.  
  204.         // Wait for something to happen
  205.         //
  206.         if(glutstuff.idlefunc == NULL)
  207.             Wait(1L<<glutstuff.msgport->mp_SigBit);
  208.  
  209.         // Handle all messages (if any)
  210.         //
  211.         idleing = TRUE;
  212.         while(msg = (struct IntuiMessage *)GetMsg(glutstuff.msgport))
  213.         {
  214.             cmsg = *msg;
  215.  
  216.             ReplyMsg((struct Message *)msg);
  217.  
  218.             stuffMakeCurrent((struct GlutWindow *)(cmsg.IDCMPWindow->UserData));
  219.  
  220.             glutstuff.curwin->qualifiers = cmsg.Qualifier;
  221.  
  222.             switch(cmsg.Class)
  223.             {
  224.                 case IDCMP_CHANGEWINDOW:
  225.                     idleing = FALSE;
  226.                     glutstuff.curwin->winx = glutstuff.curwin->window->LeftEdge;
  227.                     glutstuff.curwin->winy = glutstuff.curwin->window->TopEdge;
  228.                     glutstuff.curwin->winwidth = glutstuff.curwin->window->Width;
  229.                     glutstuff.curwin->winheight = glutstuff.curwin->window->Height;
  230.                     if((glutstuff.curwin->window->Width != glutstuff.curwin->wincurwidth) ||
  231.                        (glutstuff.curwin->window->Height != glutstuff.curwin->wincurheight))
  232.                     {
  233.                         glutstuff.curwin->wincurwidth = glutstuff.curwin->window->Width;
  234.                         glutstuff.curwin->wincurheight = glutstuff.curwin->window->Height;
  235.                         glutstuff.curwin->needreshapegui = TRUE;
  236.                         glutstuff.curwin->needredisplay = TRUE;
  237.                     }
  238.                     if((glutstuff.curwin->window->LeftEdge != glutstuff.curwin->wincurx) ||
  239.                        (glutstuff.curwin->window->TopEdge != glutstuff.curwin->wincury))
  240.                     {
  241.                         glutstuff.curwin->wincurx = glutstuff.curwin->window->LeftEdge;
  242.                         glutstuff.curwin->wincury = glutstuff.curwin->window->TopEdge;
  243.                         glutstuff.curwin->needpositiongui = TRUE;
  244.                     }
  245.                     break;
  246.                 case IDCMP_VANILLAKEY:
  247.                     if(glutstuff.curwin->keyboardfunc)
  248.                     {
  249.                         idleing = FALSE;
  250.                         glutstuff.curwin->keyboardfunc(cmsg.Code,cmsg.MouseX,cmsg.MouseY);
  251.                     }
  252.                     break;
  253.                 case IDCMP_RAWKEY:
  254.                     if(glutstuff.curwin->specialfunc)
  255.                     {
  256.                         idleing = FALSE;
  257.                         glutstuff.curwin->specialfunc(ConvRaw(cmsg.Code,cmsg.Qualifier),cmsg.MouseX,cmsg.MouseY);
  258.                     }
  259.                     break;
  260.                 case IDCMP_MENUPICK:
  261.                     menuNumber = cmsg.Code;
  262.                     while(menuNumber != MENUNULL)
  263.                     {
  264.                         item = ItemAddress(glutstuff.curwin->menu, menuNumber);
  265.                         gme = (struct GlutMenuEntry *)GTMENUITEM_USERDATA(item);
  266.                         stuffMakeCurrentMenu(gme->menu);
  267.                         if(glutstuff.curmenu->menufunc)
  268.                         {
  269.                             idleing = FALSE;
  270.                             glutstuff.curmenu->menufunc(gme->value);
  271.                         }
  272.                         menuNumber = item->NextSelect;
  273.                     }
  274.                     break;
  275.                 case IDCMP_CLOSEWINDOW:
  276.                     if(wanttoquit)
  277.                     {
  278.                         // User wants to quit, but ESC doesn't do anything
  279.                         // Panic, and return in the hope that it will drop
  280.                         // through to an end-of-program call.
  281.                         // (Note that we don't use exit(), as we may want
  282.                         // to put all this in a run-time library).
  283.                         return;
  284.                     }
  285.                     if(glutstuff.curwin->keyboardfunc)
  286.                     {
  287.                         idleing = FALSE;
  288.                         glutstuff.curwin->keyboardfunc(27,0,0);
  289.                     }
  290.                     wanttoquit = TRUE;
  291.                     break;
  292.                 case IDCMP_MOUSEBUTTONS:
  293.                     if(glutstuff.curwin->mousefunc)
  294.                     {
  295.                         idleing = FALSE;
  296.                         glutstuff.curwin->mousefunc(ConvToButton(cmsg.Code,cmsg.Qualifier),ConvToButtonState(cmsg.Code),cmsg.MouseX,cmsg.MouseY);
  297.                     }
  298.                     break;
  299.                 case IDCMP_INTUITICKS:
  300.                     if((glutstuff.curwin->mousex != cmsg.MouseX) ||
  301.                        (glutstuff.curwin->mousey != cmsg.MouseY))
  302.                     {
  303.                         glutstuff.curwin->mousex = cmsg.MouseX;
  304.                         glutstuff.curwin->mousey = cmsg.MouseY;
  305.                         if((cmsg.Qualifier & IEQUALIFIER_LEFTBUTTON) &&
  306.                            (glutstuff.curwin->motionfunc))
  307.                         {
  308.                             idleing = FALSE;
  309.                             glutstuff.curwin->motionfunc(cmsg.MouseX,cmsg.MouseY);
  310.                         }
  311.                         else if(glutstuff.curwin->passivemotionfunc)
  312.                         {
  313.                             idleing = FALSE;
  314.                             glutstuff.curwin->passivemotionfunc(cmsg.MouseX,cmsg.MouseY);
  315.                         }
  316.                     }
  317.                     break;
  318.             }
  319.         }
  320.  
  321.         // If nothing is happening, call the idle function
  322.         //
  323.         if(idleing && glutstuff.idlefunc)
  324.             glutstuff.idlefunc();
  325.     }
  326. }
  327.